package gui;

import java.util.LinkedList;
import gameManager.GameState;
import gameManager.StateEvent;
import gameManager.StateListener;
import javax.swing.table.AbstractTableModel;
import common.Piece;

public class PlayerHandTableModel extends AbstractTableModel implements StateListener {

    private boolean isUpperPlayer;
    private GameState gameState;

    public PlayerHandTableModel(GameState gameState, boolean isUpperPlayer) {
        if (gameState == null) {
            throw new NullPointerException("Game state can't be null");
        }
        this.gameState = gameState;
        this.isUpperPlayer = isUpperPlayer;
        gameState.addGameListener(this);
    }

    @Override
    public int getRowCount() {
        return 2;
    }

    @Override
    public int getColumnCount() {
        return 12;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        LinkedList<Piece> pieces = isUpperPlayer ? gameState.getUpperPlayerPieces() : gameState.getDownPlayerPieces();
        if (pieces != null && columnIndex < pieces.size()) {
            Piece piece = pieces.get(columnIndex);
            return rowIndex == 0 ? new Integer(piece.getValue1()) : piece.getValue2();
        }
        return new Integer(-1);
    }

    @Override
    public void stateChanged(StateEvent event) {
        fireTableDataChanged();
    }
}
